public class Test13 {
    public static ListNode deleteNode(ListNode head,ListNode delNode){
        if(head == null || delNode == null) return head;
        if(head == delNode) return head.next;
        if(delNode.next == null){
            ListNode temp = head;
            while(temp.next != delNode){
                temp = temp.next;
            }
            temp.next = null;
        }else{
            delNode.val = delNode.next.val;//把后一个节点值传给待删除节点
            delNode.next = delNode.next.next; //把后一个节点删除
        }
        return head;
    }

    public static void print(ListNode head){
        ListNode temp = head;
        while(temp!=null) {
            System.out.print(temp.val + " ");
            temp = temp.next;
        }
        System.out.println();
    }
    public static void main(String[] args) {


        ListNode head = new ListNode();
        head.val = 1;

        head.next = new ListNode();
        head.next.val = 2;

        head.next.next = new ListNode();
        head.next.next.val = 3;

        head.next.next.next = new ListNode();
        head.next.next.next.val = 4;

        ListNode middle = head.next.next.next.next = new ListNode();
        head.next.next.next.next.val = 5;

        head.next.next.next.next.next = new ListNode();
        head.next.next.next.next.next.val = 6;

        ListNode last =  head.next.next.next.next.next.next = new ListNode();
        head.next.next.next.next.next.next.val = 7;


        head = deleteNode(head, null); // 删除的结点为空
        print(head);
        ListNode node = new ListNode();
        node.val = 12;

        head = deleteNode(head, head); // 删除头结点
        print(head);
        head = deleteNode(head, last); // 删除尾结点
        print(head);
        head = deleteNode(head, middle); // 删除中间结点
        print(head);

        head = deleteNode(head, node); // 删除的结点不在链表中
        print(head);
    }

}
